package com.jgj.zxkj.lookandcheckjggroupinfo.record.detail;

import android.support.v7.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.sa.all_cui.mix_core.delegate.DoggerDelegate;
import com.sa.all_cui.mix_core.net.RestClient;
import com.sa.all_cui.mix_core.net.callback.IError;
import com.sa.all_cui.mix_core.net.callback.ISuccess;
import com.sa.all_cui.mix_ui.recycler.DataConvert;
import com.sa.all_cui.mix_ui.recycler.MultipleItemEntity;

import java.util.List;


/**
 * Created by all-cui on 2017/11/3.
 */

@SuppressWarnings("WeakerAccess")
public class CheckDetailHandler {
    private final RecyclerView RECYCLERVIEW;
    private final DataConvert CONVERT;
    private String mUrl = null;
    private DoggerDelegate mDelegate = null;
    private String mId = null;
    private CheckDetailAdapter mAdapter = null;

    public CheckDetailHandler(RecyclerView recyclerView,
                              DataConvert convert,
                              String url,
                              DoggerDelegate delegate,
                              String id) {
        this.RECYCLERVIEW = recyclerView;
        this.CONVERT = convert;
        this.mUrl = url;
        this.mDelegate = delegate;
        this.mId = id;
    }

    public static CheckDetailHandler create(RecyclerView recyclerView,
                                            DataConvert convert,
                                            String url,
                                            DoggerDelegate delegate,
                                            String id) {
        return new CheckDetailHandler(recyclerView, convert, url, delegate, id);
    }

    private void handlerSuccess(String response) {
        List<MultipleItemEntity> data = CONVERT.setJsonData(response).convert();
        if (mAdapter == null) {
            mAdapter = new CheckDetailAdapter(data);
            RECYCLERVIEW.setAdapter(mAdapter);
        } else {
            mAdapter.setNewData(data);
        }
    }

    public void obtainDataFromNet() {
        RestClient.builder()
                .loader(RECYCLERVIEW.getContext())
                .url(mUrl)
                .params("id", mId)
                .success(new ISuccess() {
                    @Override
                    public void onSuccess(String response) {
                        LogUtils.i(response);
                        handlerSuccess(response);
                    }
                })
                .error(new IError() {
                    @Override
                    public void onError(int code, String msg) {
                        LogUtils.i(msg);
                    }
                })
                .build()
                .get();
    }


}
